<!DOCTYPE html><html><head>
      <title>lessen_01_code</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:///c:\Users\think\.vscode\extensions\shd101wyy.markdown-preview-enhanced-0.8.13\crossnote\dependencies\katex\katex.min.css">
      
      
      
      
      
      <style>
      code[class*=language-],pre[class*=language-]{color:#333;background:0 0;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.4;-moz-tab-size:8;-o-tab-size:8;tab-size:8;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:.8em;overflow:auto;border-radius:3px;background:#f5f5f5}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal;background:#f5f5f5}.token.blockquote,.token.comment{color:#969896}.token.cdata{color:#183691}.token.doctype,.token.macro.property,.token.punctuation,.token.variable{color:#333}.token.builtin,.token.important,.token.keyword,.token.operator,.token.rule{color:#a71d5d}.token.attr-value,.token.regex,.token.string,.token.url{color:#183691}.token.atrule,.token.boolean,.token.code,.token.command,.token.constant,.token.entity,.token.number,.token.property,.token.symbol{color:#0086b3}.token.prolog,.token.selector,.token.tag{color:#63a35c}.token.attr-name,.token.class,.token.class-name,.token.function,.token.id,.token.namespace,.token.pseudo-class,.token.pseudo-element,.token.url-reference .token.variable{color:#795da3}.token.entity{cursor:help}.token.title,.token.title .token.punctuation{font-weight:700;color:#1d3e81}.token.list{color:#ed6a43}.token.inserted{background-color:#eaffea;color:#55a532}.token.deleted{background-color:#ffecec;color:#bd2c00}.token.bold{font-weight:700}.token.italic{font-style:italic}.language-json .token.property{color:#183691}.language-markup .token.tag .token.punctuation{color:#333}.language-css .token.function,code.language-css{color:#0086b3}.language-yaml .token.atrule{color:#63a35c}code.language-yaml{color:#183691}.language-ruby .token.function{color:#333}.language-markdown .token.url{color:#795da3}.language-makefile .token.symbol{color:#795da3}.language-makefile .token.variable{color:#183691}.language-makefile .token.builtin{color:#0086b3}.language-bash .token.keyword{color:#0086b3}pre[data-line]{position:relative;padding:1em 0 1em 3em}pre[data-line] .line-highlight-wrapper{position:absolute;top:0;left:0;background-color:transparent;display:block;width:100%}pre[data-line] .line-highlight{position:absolute;left:0;right:0;padding:inherit 0;margin-top:1em;background:hsla(24,20%,50%,.08);background:linear-gradient(to right,hsla(24,20%,50%,.1) 70%,hsla(24,20%,50%,0));pointer-events:none;line-height:inherit;white-space:pre}pre[data-line] .line-highlight:before,pre[data-line] .line-highlight[data-end]:after{content:attr(data-start);position:absolute;top:.4em;left:.6em;min-width:1em;padding:0 .5em;background-color:hsla(24,20%,50%,.4);color:#f4f1ef;font:bold 65%/1.5 sans-serif;text-align:center;vertical-align:.3em;border-radius:999px;text-shadow:none;box-shadow:0 1px #fff}pre[data-line] .line-highlight[data-end]:after{content:attr(data-end);top:auto;bottom:.4em}html body{font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ol,html body>ul{margin-bottom:16px}html body ol,html body ul{padding-left:2em}html body ol.no-list,html body ul.no-list{padding:0;list-style-type:none}html body ol ol,html body ol ul,html body ul ol,html body ul ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:700;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:700}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::after,html body code::before{letter-spacing:-.2em;content:'\00a0'}html body pre>code{padding:0;margin:0;word-break:normal;white-space:pre;background:0 0;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:after,html body pre code:before,html body pre tt:after,html body pre tt:before{content:normal}html body blockquote,html body dl,html body ol,html body p,html body pre,html body ul{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body code,html body pre{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview ul{list-style:disc}.markdown-preview ul ul{list-style:circle}.markdown-preview ul ul ul{list-style:square}.markdown-preview ol{list-style:decimal}.markdown-preview ol ol,.markdown-preview ul ol{list-style-type:lower-roman}.markdown-preview ol ol ol,.markdown-preview ol ul ol,.markdown-preview ul ol ol,.markdown-preview ul ul ol{list-style-type:lower-alpha}.markdown-preview .newpage,.markdown-preview .pagebreak{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center!important}.markdown-preview:not([data-for=preview]) .code-chunk .code-chunk-btn-group{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .status{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0;min-height:100vh}@media screen and (min-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{font-size:14px!important;padding:1em}}@media print{html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div,html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 300px / 2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
      <!-- The content below will be included at the end of the <head> element. --><script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function () {
    // your code here
  });
</script></head><body for="html-export">
    
    
      <div class="crossnote markdown-preview  ">
      
<h1 id="课程第一天python-基础与数学应用---练习题与答案">课程第一天：Python 基础与数学应用 - 练习题与答案 </h1>
<h2 id="内容">内容 </h2>
<h3 id="1-介绍编程和python">1. 介绍编程和Python </h3>
<h4 id="练习题">练习题 </h4>
<p><strong>Q:</strong> 什么是解释型语言？Python属于哪一种类型的语言？</p>
<p><strong>A:</strong> 解释型语言在运行时由解释器逐行解析并执行，不需要预先编译成机器码。Python是一种解释型语言。</p>
<hr>
<h3 id="2-python环境设置">2. Python环境设置 </h3>
<h4 id="练习题-1">练习题 </h4>
<p><strong>Q:</strong> 如何在命令行中运行Python脚本文件？</p>
<p><strong>A:</strong> 在命令行中，使用<code>python</code>或<code>python3</code>命令后跟脚本文件名，例如：<code>python myscript.py</code></p>
<hr>
<h3 id="3-基础语法">3. 基础语法 </h3>
<h4 id="练习题-2">练习题 </h4>
<p><strong>Q:</strong> 创建一个变量<code>a</code>赋值为10，然后创建一个变量<code>b</code>赋值为20，最后打印<code>a + b</code>的结果。</p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>a <span class="token operator">=</span> <span class="token number">10</span>
b <span class="token operator">=</span> <span class="token number">20</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span>
</code></pre><hr>
<h3 id="4-控制结构">4. 控制结构 </h3>
<h4 id="练习题-3">练习题 </h4>
<p><strong>Q:</strong> 编写一个程序，如果输入的数字大于10，则输出"大于10"，否则输出"不大于10"。</p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>num <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"请输入一个数字: "</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword keyword-if">if</span> num <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"大于10"</span><span class="token punctuation">)</span>
<span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"不大于10"</span><span class="token punctuation">)</span>
</code></pre><hr>
<h3 id="5-函数和模块">5. 函数和模块 </h3>
<h4 id="练习题-4">练习题 </h4>
<p><strong>Q:</strong> 使用<code>math</code>模块计算圆的面积，半径由用户输入。</p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> math

radius <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"请输入圆的半径: "</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
area <span class="token operator">=</span> math<span class="token punctuation">.</span>pi <span class="token operator">*</span> <span class="token punctuation">(</span>radius <span class="token operator">**</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f"圆的面积为: </span><span class="token interpolation"><span class="token punctuation">{</span>area<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>
</code></pre><hr>
<h3 id="6-用python解方程和不等式">6. 用Python解方程和不等式 </h3>
<h4 id="练习题-5">练习题 </h4>
<p><strong>Q:</strong> 使用<code>sympy</code>模块解方程<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>−</mo><mn>5</mn><mi>x</mi><mo>+</mo><mn>6</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">x^2 - 5x + 6 = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span>。</p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> Eq<span class="token punctuation">,</span> solve

x <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>
equation <span class="token operator">=</span> Eq<span class="token punctuation">(</span>x<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">-</span> <span class="token number">5</span><span class="token operator">*</span>x <span class="token operator">+</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
solution <span class="token operator">=</span> solve<span class="token punctuation">(</span>equation<span class="token punctuation">,</span> x<span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f"方程的解为: </span><span class="token interpolation"><span class="token punctuation">{</span>solution<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>
</code></pre><p><strong>Q:</strong> 使用<code>sympy</code>模块解方程组<br>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo fence="true">{</mo><mtable rowspacing="0.36em" columnalign="left left" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mn>3</mn><mi>x</mi><mo>+</mo><mn>2</mn><mi>y</mi><mo>=</mo><mn>7</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mi>x</mi><mo>−</mo><mi>y</mi><mo>=</mo><mn>1</mn></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\begin{cases}
3x + 2y = 7 \\
x - y = 1
\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:3em;vertical-align:-1.25em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.69em;"><span style="top:-3.69em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord">3</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord">2</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">7</span></span></span><span style="top:-2.25em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.19em;"><span></span></span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> Eq<span class="token punctuation">,</span> solve

<span class="token comment"># 定义符号</span>
x<span class="token punctuation">,</span> y <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x y'</span><span class="token punctuation">)</span>

<span class="token comment"># 定义方程</span>
eq1 <span class="token operator">=</span> Eq<span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">*</span>x <span class="token operator">+</span> <span class="token number">2</span><span class="token operator">*</span>y<span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span>
eq2 <span class="token operator">=</span> Eq<span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>

<span class="token comment"># 使用solve()函数求解方程组</span>
solution <span class="token operator">=</span> solve<span class="token punctuation">(</span><span class="token punctuation">(</span>eq1<span class="token punctuation">,</span> eq2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution<span class="token punctuation">)</span>
</code></pre><p><strong>Q:</strong> 使用<code>sympy</code>模块解不等式</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>2</mn><mi>x</mi><mo>−</mo><mn>3</mn><mo>&gt;</mo><mn>5</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">2x - 3 &gt; 5x + 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6835em;vertical-align:-0.0391em;"></span><span class="mord">3</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">2</span></span></span></span></span></p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> solveset<span class="token punctuation">,</span> S

<span class="token comment"># 定义符号</span>
x <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>

<span class="token comment"># 定义不等式</span>
inequality <span class="token operator">=</span> <span class="token number">2</span><span class="token operator">*</span>x <span class="token operator">-</span> <span class="token number">3</span> <span class="token operator">&gt;</span> <span class="token number">5</span><span class="token operator">*</span>x <span class="token operator">+</span> <span class="token number">2</span>

<span class="token comment"># 使用solveset()函数求解不等式</span>
solution_set <span class="token operator">=</span> solveset<span class="token punctuation">(</span>inequality<span class="token punctuation">,</span> x<span class="token punctuation">,</span> domain<span class="token operator">=</span>S<span class="token punctuation">.</span>Reals<span class="token punctuation">)</span>

<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution_set<span class="token punctuation">)</span>

</code></pre><p>使用SymPy求解得到：</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>x</mi><mo>∈</mo><mo stretchy="false">(</mo><mo>−</mo><mi mathvariant="normal">∞</mi><mo separator="true">,</mo><mo>−</mo><mfrac><mn>5</mn><mn>3</mn></mfrac><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">x \in (-\infty, -\frac{5}{3})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mopen">(</span><span class="mord">−</span><span class="mord">∞</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">−</span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">3</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">5</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mclose">)</span></span></span></span></span></p>
<p>这意味着满足原不等式的<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>的范围是所有小于<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>−</mo><mfrac><mn>5</mn><mn>3</mn></mfrac></mrow><annotation encoding="application/x-tex">-\frac{5}{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.1901em;vertical-align:-0.345em;"></span><span class="mord">−</span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8451em;"><span style="top:-2.655em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.394em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">5</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.345em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span>的实数。</p>
<h3 id="练习题猜数字游戏">练习题：猜数字游戏 </h3>
<p><strong>Q:</strong> 编写一个猜数字游戏，程序随机生成一个1至10之间的整数，玩家需要通过提示来猜测这个数字。如果猜错了，告诉玩家是猜大了还是猜小了。</p>
<p><strong>A:</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> random

<span class="token keyword keyword-def">def</span> <span class="token function">guess_number_game</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    number_to_guess <span class="token operator">=</span> random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span>
    guess <span class="token operator">=</span> <span class="token boolean">None</span>
    attempts <span class="token operator">=</span> <span class="token number">0</span>
    
    <span class="token keyword keyword-while">while</span> guess <span class="token operator">!=</span> number_to_guess<span class="token punctuation">:</span>
        guess <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"猜一个1到10之间的数字: "</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        attempts <span class="token operator">+=</span> <span class="token number">1</span>
        
        <span class="token keyword keyword-if">if</span> guess <span class="token operator">&lt;</span> number_to_guess<span class="token punctuation">:</span>
            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"太小了！再试一次。"</span><span class="token punctuation">)</span>
        <span class="token keyword keyword-elif">elif</span> guess <span class="token operator">&gt;</span> number_to_guess<span class="token punctuation">:</span>
            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"太大了！再试一次。"</span><span class="token punctuation">)</span>
    
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f"恭喜你！你猜对了数字</span><span class="token interpolation"><span class="token punctuation">{</span>number_to_guess<span class="token punctuation">}</span></span><span class="token string">。你总共尝试了</span><span class="token interpolation"><span class="token punctuation">{</span>attempts<span class="token punctuation">}</span></span><span class="token string">次。"</span></span><span class="token punctuation">)</span>

guess_number_game<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><p>为了增加游戏的趣味性和挑战性，我们可以向背包中添加一个特殊道具——“魔法药水”，玩家可以在遇到怪物时使用它来暂时击退怪物，从而安全地离开。下面是修改后的游戏代码，其中增加了“魔法药水”的使用逻辑。</p>
<h3 id="练习题文字冒险游戏">练习题：文字冒险游戏 </h3>
<p><strong>目标：</strong></p>
<ul>
<li>设计一个至少包括三个地点的冒险游戏，玩家需要找到迷宫内的宝藏来获取胜利</li>
<li>玩家需要输入指令来控制角色行动。</li>
<li>迷宫内有怪物，背包中包含“魔法药水”，可在遇到怪物时使用。</li>
</ul>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-def">def</span> <span class="token function">adventure_game</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"欢迎来到迷宫探险！你的目标是找到出口。"</span><span class="token punctuation">)</span>
    location <span class="token operator">=</span> <span class="token string">'start'</span>
    backpack <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'map'</span><span class="token punctuation">:</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">'magic_potion'</span><span class="token punctuation">:</span> <span class="token boolean">True</span><span class="token punctuation">}</span>

    <span class="token keyword keyword-def">def</span> <span class="token function">check_backpack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你的背包中有："</span><span class="token punctuation">)</span>
        <span class="token keyword keyword-for">for</span> item<span class="token punctuation">,</span> has_it <span class="token keyword keyword-in">in</span> backpack<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> has_it<span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f"- </span><span class="token interpolation"><span class="token punctuation">{</span>item<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>

    <span class="token keyword keyword-while">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        <span class="token keyword keyword-if">if</span> location <span class="token operator">==</span> <span class="token string">'start'</span><span class="token punctuation">:</span>
            action <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"你现在站在迷宫的入口，你可以向前走，输入'forward'；检查背包，输入'check bag'；或者查看地图，输入'map'："</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-if">if</span> action <span class="token operator">==</span> <span class="token string">'forward'</span><span class="token punctuation">:</span>
                location <span class="token operator">=</span> <span class="token string">'hallway'</span>
            <span class="token keyword keyword-elif">elif</span> action <span class="token operator">==</span> <span class="token string">'check bag'</span><span class="token punctuation">:</span>
                check_backpack<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-elif">elif</span> action <span class="token operator">==</span> <span class="token string">'map'</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你查看了地图，发现迷宫大致呈L形，你目前在起点。"</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"无效的指令，请重新输入。"</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-elif">elif</span> location <span class="token operator">==</span> <span class="token string">'hallway'</span><span class="token punctuation">:</span>
            direction <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"你来到了一条长廊，你可以向左转，输入'left'；向右转，输入'right'；前往神秘房间，输入'mystery'；或者返回入口，输入'back'："</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-if">if</span> direction <span class="token operator">==</span> <span class="token string">'left'</span><span class="token punctuation">:</span>
                location <span class="token operator">=</span> <span class="token string">'treasure_room'</span>
            <span class="token keyword keyword-elif">elif</span> direction <span class="token operator">==</span> <span class="token string">'right'</span><span class="token punctuation">:</span>
                location <span class="token operator">=</span> <span class="token string">'monster_room'</span>
            <span class="token keyword keyword-elif">elif</span> direction <span class="token operator">==</span> <span class="token string">'mystery'</span><span class="token punctuation">:</span>
                location <span class="token operator">=</span> <span class="token string">'mystery_room'</span>
            <span class="token keyword keyword-elif">elif</span> direction <span class="token operator">==</span> <span class="token string">'back'</span><span class="token punctuation">:</span>
                location <span class="token operator">=</span> <span class="token string">'start'</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"无效的指令，请重新输入。"</span><span class="token punctuation">)</span>

        <span class="token keyword keyword-elif">elif</span> location <span class="token operator">==</span> <span class="token string">'treasure_room'</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你找到了一间藏宝室，里面堆满了金币和宝石。"</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-break">break</span>

        <span class="token keyword keyword-elif">elif</span> location <span class="token operator">==</span> <span class="token string">'monster_room'</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> backpack<span class="token punctuation">[</span><span class="token string">'magic_potion'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你不幸遇到了一只可怕的怪物！"</span><span class="token punctuation">)</span>
                use_potion <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"你是否要使用魔法药水击退怪物？输入'y'或'n'："</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-if">if</span> use_potion<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'y'</span><span class="token punctuation">:</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"魔法药水生效，怪物被击退了！"</span><span class="token punctuation">)</span>
                    backpack<span class="token punctuation">[</span><span class="token string">'magic_potion'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
                    location <span class="token operator">=</span> <span class="token string">'hallway'</span>
                <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你没有使用魔法药水，被怪物攻击，游戏结束。"</span><span class="token punctuation">)</span>
                    <span class="token keyword keyword-break">break</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你没有魔法药水，无法击退怪物，游戏结束。"</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-break">break</span>

        <span class="token keyword keyword-elif">elif</span> location <span class="token operator">==</span> <span class="token string">'mystery_room'</span><span class="token punctuation">:</span>
            <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> backpack<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'torch'</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你进入了一个神秘的房间，里面一片漆黑。"</span><span class="token punctuation">)</span>
                take_torch <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"你找到了一支手电筒，你是否要拿走它？输入'y'或'n'："</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-if">if</span> take_torch<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'y'</span><span class="token punctuation">:</span>
                    backpack<span class="token punctuation">[</span><span class="token string">'torch'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你拿起手电筒，现在可以照亮黑暗的地方了。"</span><span class="token punctuation">)</span>
                <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你决定不拿手电筒，继续前进。"</span><span class="token punctuation">)</span>
                location <span class="token operator">=</span> <span class="token string">'hallway'</span>
            <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
                <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"你再次进入了神秘的房间，但是这里似乎没有什么新的东西。"</span><span class="token punctuation">)</span>
                location <span class="token operator">=</span> <span class="token string">'hallway'</span>


adventure_game<span class="token punctuation">(</span><span class="token punctuation">)</span>

</code></pre><h3 id="sympy-解方程-方程组和不等式的习题">Sympy 解方程、方程组和不等式的习题 </h3>
<h4 id="习题-1-解单一方程">习题 1: 解单一方程 </h4>
<p>使用 <code>sympy</code> 解下列方程：<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>x</mi><mo>+</mo><mn>3</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">x^2 - 4x + 3 = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9474em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">4</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">3</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span></span></p>
<p><strong>解答：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> sympy <span class="token keyword keyword-as">as</span> sp

x <span class="token operator">=</span> sp<span class="token punctuation">.</span>symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>
equation <span class="token operator">=</span> x<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">-</span> <span class="token number">4</span><span class="token operator">*</span>x <span class="token operator">+</span> <span class="token number">3</span>
solution <span class="token operator">=</span> sp<span class="token punctuation">.</span>solve<span class="token punctuation">(</span>equation<span class="token punctuation">,</span> x<span class="token punctuation">)</span>
solution
</code></pre><h4 id="习题-2-解方程组">习题 2: 解方程组 </h4>
<p>给定方程组：<br>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo fence="true">{</mo><mtable rowspacing="0.36em" columnalign="left left" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mn>1</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mi>x</mi><mo>−</mo><mi>y</mi><mo>=</mo><mn>3</mn></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\begin{cases}
x + y = 1 \\
x - y = 3
\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:3em;vertical-align:-1.25em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.69em;"><span style="top:-3.69em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">1</span></span></span><span style="top:-2.25em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">3</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.19em;"><span></span></span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span><br>
使用 <code>sympy</code> 找出 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span> 和 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi></mrow><annotation encoding="application/x-tex">y</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span> 的值。</p>
<p><strong>解答：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> sympy <span class="token keyword keyword-as">as</span> sp
x<span class="token punctuation">,</span> y <span class="token operator">=</span> sp<span class="token punctuation">.</span>symbols<span class="token punctuation">(</span><span class="token string">'x y'</span><span class="token punctuation">)</span>
eq1 <span class="token operator">=</span> x <span class="token operator">+</span> y <span class="token operator">-</span> <span class="token number">1</span>
eq2 <span class="token operator">=</span> x <span class="token operator">-</span> y <span class="token operator">-</span> <span class="token number">3</span>
solution <span class="token operator">=</span> sp<span class="token punctuation">.</span>solve<span class="token punctuation">(</span><span class="token punctuation">(</span>eq1<span class="token punctuation">,</span> eq2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution<span class="token punctuation">)</span>
</code></pre><h4 id="习题-3-解不等式">习题 3: 解不等式 </h4>
<p>求解不等式：<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>−</mo><mn>3</mn><mi>x</mi><mo>−</mo><mn>10</mn><mo>&lt;</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">x^2 - 3x - 10 &lt; 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9474em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">3</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6835em;vertical-align:-0.0391em;"></span><span class="mord">10</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span></span></p>
<p><strong>解答：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> sympy <span class="token keyword keyword-as">as</span> sp
x <span class="token operator">=</span> sp<span class="token punctuation">.</span>symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>
inequality <span class="token operator">=</span> x<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">-</span> <span class="token number">3</span><span class="token operator">*</span>x <span class="token operator">-</span> <span class="token number">10</span>
solution <span class="token operator">=</span> sp<span class="token punctuation">.</span>solve_univariate_inequality<span class="token punctuation">(</span>inequality <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution<span class="token punctuation">)</span>
</code></pre><h4 id="习题-4-解参数化方程">习题 4: 解参数化方程 </h4>
<p>解方程 <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mi>x</mi><mo>+</mo><mi>c</mi><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">ax^2 + bx + c = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9474em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8641em;"><span style="top:-3.113em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span></span> 其中 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi></mrow><annotation encoding="application/x-tex">a</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">a</span></span></span></span>, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi></mrow><annotation encoding="application/x-tex">b</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span></span></span></span>, 和 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi></mrow><annotation encoding="application/x-tex">c</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">c</span></span></span></span> 是参数。</p>
<p><strong>解答：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> sympy <span class="token keyword keyword-as">as</span> sp
a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> x <span class="token operator">=</span> sp<span class="token punctuation">.</span>symbols<span class="token punctuation">(</span><span class="token string">'a b c x'</span><span class="token punctuation">)</span>
equation <span class="token operator">=</span> a<span class="token operator">*</span>x<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">+</span> b<span class="token operator">*</span>x <span class="token operator">+</span> c
solution <span class="token operator">=</span> sp<span class="token punctuation">.</span>solve<span class="token punctuation">(</span>equation<span class="token punctuation">,</span> x<span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution<span class="token punctuation">)</span>
</code></pre><h3 id="运行代码获得答案">运行代码获得答案 </h3>
<p>运行上述代码片段，你会得到如下答案：</p>
<h4 id="习题-1-的答案">习题 1 的答案： </h4>
<pre data-role="codeBlock" data-info="" class="language-text"><code>[1, 3]
</code></pre><h4 id="习题-2-的答案">习题 2 的答案： </h4>
<pre data-role="codeBlock" data-info="" class="language-text"><code>{x: 2, y: -1}
</code></pre><h4 id="习题-3-的答案">习题 3 的答案： </h4>
<pre data-role="codeBlock" data-info="" class="language-text"><code>(-2 &lt; x) &amp; (x &lt; 5)
</code></pre><h4 id="习题-4-的答案">习题 4 的答案： </h4>
<pre data-role="codeBlock" data-info="" class="language-text"><code>[-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a), -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a)]
</code></pre>
      </div>
      
      
    
    
    
    
    
    
  
    </body></html>